package jmine.tec.hydra.services.incluir;

import jmine.tec.database.pack.PackException;
import jmine.tec.database.pack.services.PackEntityPersistenceHelper;
import jmine.tec.di.annotation.Injected;
import jmine.tec.hydra.domain.HydraActivation;
import jmine.tec.hydra.domain.HydraSystem;
import jmine.tec.script.persistent.domain.Script;
import jmine.tec.services.annotations.Execution;
import jmine.tec.services.annotations.Input;
import jmine.tec.services.annotations.Output;
import jmine.tec.services.annotations.ServiceImplementor;
import jmine.tec.services.api.ActionsEnum;
import jmine.tec.services.api.export.ReferenceMap;
import jmine.tec.services.api.export.ServiceFiller;
import jmine.tec.services.export.impl.ExportUtils;
import jmine.tec.services.io.ServiceBean;

/**
 * Serviço para inclusão de uma ativação de integração.
 * 
 * @author piercio
 */
@ServiceImplementor(action = ActionsEnum.INCLUIR)
public class HydraActivationService implements ServiceFiller<HydraActivation> {

    public static final String IDENTIFICADOR = "Identificador";

    public static final String MNEMONICO = "Mnemônico";

    public static final String SISTEMA = "Sistema";

    public static final String SCRIPT = "Script";

    public static final String DATABASE_ID = "Database ID";

    // Injected
    private PackEntityPersistenceHelper helper;

    // Opcional
    private Long databaseID;

    /**
     * Executa o serviço e cria um {@link HydraActivation}
     * 
     * @param mnemonico mnemonico
     * @param script script
     * @param system system
     * @return o sistema de integração.
     * @throws PackException PackException
     */
    @Execution
    @Output(propertyName = IDENTIFICADOR)
    public HydraActivation executeService(@Input(fieldName = MNEMONICO) String mnemonico, @Input(fieldName = SCRIPT) Script script,
            @Input(fieldName = SISTEMA) HydraSystem system) throws PackException {
        HydraActivation activation = this.helper.findOrCreate(HydraActivation.class, this.databaseID, mnemonico);
        activation.setMnemonico(mnemonico);
        activation.setActivationScript(script);
        activation.setSystem(system);
        this.helper.save(activation, this.databaseID);
        return activation;
    }

    /**
     * {@inheritDoc}
     */
    public void fillServiceBean(ServiceBean bean, ReferenceMap referenceMap, HydraActivation entity) {
        ExportUtils.setService(bean, this.getClass());
        ExportUtils.put(bean, MNEMONICO, entity.getMnemonico());
        ExportUtils.put(bean, SISTEMA, referenceMap, entity.getSystem(), entity.getSystem().getMnemonico());
        ExportUtils.put(bean, SCRIPT, entity.getActivationScript().getMnemonico());
        ExportUtils.putDatabaseId(bean, DATABASE_ID, entity);
        ExportUtils.put(bean, IDENTIFICADOR, referenceMap.addReference(entity, "hydraActivation"));
    }

    /**
     * @param helper the helper to set
     */
    @Injected
    public void setHelper(PackEntityPersistenceHelper helper) {
        this.helper = helper;
    }

    /**
     * @param databaseID the databaseID to set
     */
    @Input(fieldName = DATABASE_ID, required = false)
    public void setDatabaseID(Long databaseID) {
        this.databaseID = databaseID;
    }
}
